package com.zijie.shopgoodsapi.goods.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.core.util.JsonResult;
import com.zijie.goods.model.Goods;
import com.zijie.goods.model.search.GoodSearchBean;
import com.zijie.goods.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/goods")
public class GoodsApi {
    private GoodService goodService;

    @Autowired
    public void setBrandService(GoodService goodService) {
        this.goodService = goodService;
    }
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(
            @RequestParam(defaultValue = "1")Integer pageNo,
            @RequestParam(defaultValue = "10")Integer pageSize
            , GoodSearchBean gsb) {
//        System.out.println(pageNo+pageSize);
        Page<Goods> page = Page.of(pageNo, pageSize);
        page=goodService.findAll(page, gsb);
        return ResponseEntity.ok(JsonResult.success(page));
    }
//    添加
    @PostMapping("/add")
    public ResponseEntity<JsonResult<?>> add(@RequestBody Goods goods) {
        return ResponseEntity.ok(JsonResult.success(goodService.save(goods)));
    }
//    修改
    @PutMapping("/update")
    public ResponseEntity<JsonResult<?>> update(@RequestBody Goods goods) {
        return ResponseEntity.ok(JsonResult.success(goodService.update(goods)));
    }
//    删除多个
    @DeleteMapping("/delete")
    public ResponseEntity<JsonResult<?>> delete(@RequestParam("ids") List<Integer> ids) {
        return ResponseEntity.ok(JsonResult.success(goodService.deleteByIds(ids)));
    }
    //删除单个
    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<JsonResult<?>> deleteById(@PathVariable Integer id) {
        return ResponseEntity.ok(JsonResult.success(goodService.deleteById(id)));
    }
    @GetMapping("/findById/{id}")
    public ResponseEntity<JsonResult<?>> findById(@PathVariable Integer id) {
        Goods goods = goodService.findById(id);
        if (goods == null) {
            return ResponseEntity.ok(JsonResult.error("未找到该品牌"));
        }
        return ResponseEntity.ok(JsonResult.success(goods));
    }
    //查询热销
        @GetMapping("/findHotByCount/{count}")
    public ResponseEntity<JsonResult<List<Goods>>> findAllHot(@PathVariable int count) {
        Page<Goods> page = new Page<>(1, count);
        GoodSearchBean goods = new GoodSearchBean();
        goods.setIsHot(true);
        List<Goods> list = goodService.findAll(page, goods).getRecords();
        return ResponseEntity.ok(JsonResult.success(list));
    }
    //查询新品
    @GetMapping("/findNewByCount/{count}")
    public ResponseEntity<JsonResult<List<Goods>>> findNewByCount(@PathVariable int count) {
        Page<Goods> page = new Page<>(1, count);
        GoodSearchBean goods = new GoodSearchBean();
        goods.setIsTakeDown(true);
        List<Goods> list = goodService.findAll(page, goods).getRecords();
        return ResponseEntity.ok(JsonResult.success(list));
    }
    //更新热门状态
    @PutMapping("/updateHotStatus/{id}/{hotStatus}")
    public ResponseEntity<JsonResult<?>> updateNewStatus(@PathVariable Integer id,@PathVariable Integer  hotStatus) {
        return ResponseEntity.ok(JsonResult.success(goodService.updateHotStatus(id, hotStatus)));
    }
    //更新下架状态
    @PutMapping("/updatePublicStatus/{id}/{isTakeDown}")
    public ResponseEntity<JsonResult<?>> updatePublicStatus(@PathVariable Integer id,@PathVariable Integer  isTakeDown) {
        return ResponseEntity.ok(JsonResult.success(goodService.updatePublicStatus(id, isTakeDown)));
    }

}